
/* 通过ID获取枚举文本 */
def getEnumTxtBy(id){

	def sqlClient = null;
	
	try {
	
		sqlClient= getSQLClient();
		
		def row = sqlClient.firstRow('select showvalue from ctp_enum_item where id=?', [id]);
		
		if(null == row){
			return id;
		}
		
		return row.showvalue;
	} catch(Exception e){
		return id;
	} finally {
		if(null != sqlClient){
			try{
				sqlClient.close();
			} catch(Throwable e){}
		}
	}
}


/* 通过ID获取枚举值 */
def getEnumvalueBy(id){

	def sqlClient = null;
	
	try {
	
		sqlClient= getSQLClient();
		
		def row = sqlClient.firstRow('select enumvalue from ctp_enum_item where id=?', [id]);
		
		if(null == row){
			return id;
		}
		
		return row.enumvalue;
	} catch(Exception e){
		return id;
	} finally {
		if(null != sqlClient){
			try{
				sqlClient.close();
			} catch(Throwable e){}
		}
	}
}

/* 通过ID获取姓名 */
def getMemberNameBy(id){
	
	def sqlClient = null;
	
	try {
	
		sqlClient= getSQLClient();
		
		def row = sqlClient.firstRow('select name from org_member where id=?', [id]);
		
		if(null == row){
			return id;
		}
		
		return row.name;
	} catch(Exception e){
		return id;
	} finally {
		if(null != sqlClient){
			try{
				sqlClient.close();
			} catch(Throwable e){}
		}
	}
}


/* 通过ID获取登录名 */
def getLoginnameBy(id){
	
	def sqlClient = null;
	
	try {
	
		sqlClient= getSQLClient();
		
		def row = sqlClient.firstRow('select login_name from org_principal where member_id=?', [id]);
		
		if(null == row){
			return id;
		}
		
		return row.login_name;
	} catch(Exception e){
		return id;
	} finally {
		if(null != sqlClient){
			try{
				sqlClient.close();
			} catch(Throwable e){}
		}
	}
}

/* 基于 SQL 获取数据 */
def getObjBySQL(sqlStr, closure, nodata_closure=null){
	
	getObjBySQL(sqlStr, null, closure, nodata_closure);
}

/* 基于 SQL 获取数据 */
def getObjBySQL(sqlStr, args, closure, nodata_closure){

	def sqlClient = null;
	
	try {
		
		sqlClient = getSQLClient();
		
		def row = (null == args ? sqlClient.firstRow(sqlStr) : sqlClient.firstRow(sqlStr, args));
		
		sqlClient.close();
		sqlClient = null;
		
		if(null != row){
			closure(row);
		}else {
			if(null != nodata_closure) {
				nodata_closure();
			}
		}
	} finally {
		if(null != sqlClient) {
			sqlClient.close();
		}
	}
}

/* 基于 SQL 获取数据列表 */
def getListBySQL(sqlStr, closure, nodata_closure=null){
	
	getListBySQL(sqlStr, null, closure, nodata_closure);
}

/* 基于 SQL 获取数据列表 */
def getListBySQL(sqlStr, args, closure, nodata_closure){

	def sqlClient = null;
	
	try {
		
		sqlClient = getSQLClient();
		
		def row = (null == args ? sqlClient.rows(sqlStr) : sqlClient.rows(sqlStr, args));
		
		sqlClient.close();
		sqlClient = null;
		
		if(null != row && 0 < row.size()){
			for(r in row){
				closure(r);
			}
		}else {
			if(null != nodata_closure) {
				nodata_closure();
			}
		}
	} finally {
		if(null != sqlClient) {
			sqlClient.close();
		}
	}
}


/* 获取SQL客户端 */
def getSQLClient(){
	return new groovy.sql.Sql(com.seeyon.ctp.common.AppContext.getBean("dataSource"));
}

/**
 * 发送消息
 */
def sendMessage(txt, receiver=null){
	
	try {
		
		if(snail.common.utils.StringHelper.isNotBlank(receiver)) {
			
			com.seeyon.apps.snail.common.MessageHepler.send(txt, receiver.split('\\s*[,;]\\s*'));
			
		}else {
			logger("发送消息失败！[TXT: ${txt}, receiver: null]");
		}
	} catch(Throwable e){
		
		logger("发送消息失败！[TXT: ${txt}, ERROR: ${e.message}]");
	}
}

/* 输出日志 */
def logger(txt){
	com.seeyon.apps.snail.APPLogger.info(txt);
}

/* 获取TOKEN */ 
def getToken(username=null, password=null, proxyuser=null){

	def token = null;
	
	try{
		
		token = getCurrentToken();
		
		if(!snail.common.utils.StringHelper.isBlank(token)){
			return token;
		}
	} catch(Throwable e){}
	
	try {
		
		token = snail.common.utils.CacheHelper.get("__TOKEN_CACHE__");
		
		if(snail.common.utils.StringHelper.isBlank(token)){
			
			if(null == username) {
				username = GLOBALS('__SNAIL_REST_USER__');
			}
			
			if(null == password) {
				password = GLOBALS('__SNAIL_REST_PASSWORD__');
			}
			
			if(null == proxyuser) {
				proxyuser = GLOBALS('__SNAIL_REST_AGENCY__');
			}
			
			token = snail.common.web.HTTPHelper.get(
					"${getRestHost()}/token/${username}/${password}?loginName=${proxyuser}",
					null,
					['Accept': 'text/plain']
				);
			
			snail.common.utils.CacheHelper.put("__TOKEN_CACHE__", token, 600000L);
		}
		
	} catch(Exception e){}
	
	return token;
}


/* 获取TOKEN */
def getCurrentToken(){
	try {
		return com.seeyon.ctp.rest.filter.AuthorizationRequestFilter.getToken(com.seeyon.ctp.common.web.util.WebUtil.getRequest());
	} catch(Throwable e){
		throw new ServiceException('获取TOKEN失败！');
	}
}


/* 获取TOKEN */ 
def getRestHost(){
	return GLOBALS('__SNAIL_REST_HOST__') + '/seeyon/rest';
}

/* 从脚本上下文中获取变量值 */ 
def getScriptContextData(key, defaultValue){

    def context = (null != __SCRIPT_ORIGINAL_DATA__) ? __SCRIPT_ORIGINAL_DATA__ : LocalCacheHelper.get('__SCRIPT_ORIGINAL_DATA__');
    	
	def value = (null != context) ? context.get(key) :  LocalCacheHelper.get(key);
	
	return null != value ? value : defaultValue;
}

/* 模拟全局变量 */ 
def GLOBALS(key, value=null) {
	
	__SCRIPT_ORIGINAL_DATA__['__LOCAL_THREAD_CACHE__'] = NVL(__SCRIPT_ORIGINAL_DATA__['__LOCAL_THREAD_CACHE__'], [:]);
	
	if(null == value) {
		return __SCRIPT_ORIGINAL_DATA__['__LOCAL_THREAD_CACHE__'][key];
	}else {
		return __SCRIPT_ORIGINAL_DATA__['__LOCAL_THREAD_CACHE__'][key] = value;
	}
}

/* 取不为空的对象，如果都为空，则返回最后一个对象 */ 
def NVL(obj, Object... obj2){
	
	if(snail.common.ClassHelper.isBlank(obj)) {
		
		def tempObj = null;
		
		for(o in obj2){
			
			tempObj = o;
			
			if(snail.common.ClassHelper.isBlank(o)) {
			
				continue;
			}
			
			return o;
		}
		
		return tempObj;
	}
	
	return obj;
}




